package com.google.code.netcheckin.aprs;

import com.google.code.netcheckin.io.Ascii;
import com.google.code.netcheckin.util.ByteUtils;

public class AprsPacketDataObject extends AprsPacketData {

	public static final byte OBJECT_CREATE = Ascii.V_ASTERISK;
	public static final byte OBJECT_KILL = Ascii.V_UNDERSCORE;

	protected byte[] objectName;
	protected byte	objectStatus = OBJECT_CREATE;
	protected long  timeStamp;
	protected LatOrLong latitude;
	protected LatOrLong longitude;
	protected byte[] comment;

	public AprsPacketDataObject() {
		super();
		dataType = AprsPacketConstants.OBJECT;
	}

		/**
		  * Pass a formater that can be used to format fields in the packet.
		  * Optional. If not provided, one will be instantiated when needed.
		  *
		*/

	public AprsPacketDataObject(AprsFormatter formatter) {
		super(formatter);
		dataType = AprsPacketConstants.OBJECT;
	}

	public final byte[] getComment() {
		return comment;
	}

	public final LatOrLong getLatitude() {
		return latitude;
	}

	public final LatOrLong getLongitude() {
		return longitude;
	}

	public final byte[] getObjectName() {
		return objectName;
	}

	public final byte getObjectStatus() {
		return objectStatus;
	}

	public final long getTimestamp() {
		return timeStamp;
	}

	public final void setComment(String comment) {
		setComment(comment.getBytes());
	}

	public final void setComment(byte[] comment) {
		this.comment = comment;
	}

	public final void setLatitude(LatOrLong lat) {
		latitude = lat;
	}

	public final void setLongitude(LatOrLong longit) {
		longitude = longit;
	}

	public final void setObjectName(byte[] objname) {
		objectName = objname;
	}

	public final void setObjectStatus(byte status) {
		objectStatus = status;
	}

	public final void setTimestamp(long stamp) {
		timeStamp = stamp;
	}

	public final byte[] toPacket() {
		byte[] answer = new byte[128];
		int offset = super.addPacketBytes(answer, 0);
		offset = addPacketBytes(answer, offset);
		return ByteUtils.subArray(answer, 0, offset);
	}

	////////////////////// protected below ////////////////////////

	protected int addPacketBytes(byte[] buffer, int offset) {
		AprsFormatter fmthold = formatter;
		if (formatter == null) {
			formatter = new AprsFormatter();
		}
		buffer[offset] = AprsPacketConstants.OBJECT;
		offset ++;
		offset = ByteUtils.copyIntoFixedPadded(buffer, offset, 9, objectName);
		buffer[offset] = objectStatus;
		offset ++;
		offset = ByteUtils.copyInto(buffer, offset, formatter.formatAprsTimestamp(timeStamp).getBytes());
		offset = ByteUtils.copyInto(buffer, offset, latitude.toPacketBytes(formatter));
		buffer[offset] = getSymbolTable();
		offset ++;
		offset = ByteUtils.copyInto(buffer, offset, longitude.toPacketBytes(formatter));
		buffer[offset] = getSymbolId();
		offset ++;
		if (comment != null && comment.length > 0) {
			if (comment.length < 44) {
				offset = ByteUtils.copyInto(buffer, offset, comment);
			} else {
				offset = ByteUtils.copyInto(buffer, offset, ByteUtils.subArray(comment, 0, 43));
			}
		}

		formatter = fmthold;
		return offset;
	}

}
